package com.yuxl.common.auth.controller;

import com.yuxl.common.auth.entity.AuthMenu;
import com.yuxl.common.auth.service.AuthMenuService;
import com.yuxl.common.auth.service.SecurityService;
import com.yuxl.common.core.domain.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

@Tag(name = "菜单接口")
@RequestMapping("/menu")
@RestController
public class AuthMenuController {

    @Resource
    private AuthMenuService authMenuService;

    @Operation(summary = "创建菜单")
    @PostMapping
    public Mono<AuthMenu> create(@RequestBody AuthMenu entity) {
        return authMenuService.create(entity);
    }

    @Operation(summary = "根据ID查询菜单")
    @GetMapping("/{id}")
    public Mono<R<AuthMenu>> getById(@Parameter(description = "菜单ID") @PathVariable String name) {
        return authMenuService.findById(name)
                .map(R::ok);
    }

    @Operation(summary = "更新菜单")
    @PutMapping
    public Mono<R<AuthMenu>> updateById(@RequestBody AuthMenu entity) {
        return authMenuService.updateById(entity)
                .map(R::ok);
    }

    @Operation(summary = "删除菜单")
    @DeleteMapping("/{id}")
    public Mono<R<Void>> deleteById(@Parameter(description = "菜单ID") @PathVariable String name) {
        return authMenuService.deleteById(name)
                .thenReturn(R.ok());
    }

    @Operation(summary = "条件查询菜单列表")
    @PostMapping("/list")
    public Mono<R<List<AuthMenu>>> list(@RequestBody AuthMenu entity) {
        return authMenuService.list(entity)
                .collectList()
                .map(R::ok);
    }

    @Operation(summary = "统计菜单数量")
    @PostMapping("/count")
    public Mono<R<Integer>> count(@RequestBody AuthMenu entity) {
        return authMenuService.count(entity).map(R::ok);
    }

    @Operation(summary = "分页条件查询菜单")
    @PostMapping("/{page}/{size}/page")
    public Mono<R<List<AuthMenu>>> page(
            @RequestBody AuthMenu entity,
            @PathVariable int page,
            @PathVariable int size
    ) {
        return authMenuService.page(entity, page, size)
                .collectList()
                .map(R::ok);
    }


    @Operation(summary = "查询菜单树")
    @PostMapping("/tree")
    public Mono<R<List<AuthMenu>>> tree(@RequestBody List<String> menuNames) {
        return authMenuService.tree(menuNames)
                .map(R::ok);
    }

}
